package com.pirestupppai.ui.fragment;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.pirestupppai.R;
import com.pirestupppai.adapter.PsListAdapter;
import com.pirestupppai.base.BaseFragment;
import com.pirestupppai.bean.LoginUser;
import com.pirestupppai.bean.TupaiUser;
import com.pirestupppai.common.Constant;
import com.pirestupppai.network.NetworkRequest;
import com.pirestupppai.network.TupaiSubscriber;
import com.pirestupppai.rxbus.RxBus;
import com.pirestupppai.ui.activity.CollectionActivity;
import com.pirestupppai.ui.activity.FollowListActivity;
import com.pirestupppai.ui.activity.MessageListActivity;
import com.pirestupppai.ui.activity.PsListActivity;
import com.pirestupppai.ui.activity.SettingActivity;
import com.pirestupppai.ui.activity.UserActivity;
import com.pirestupppai.utils.ImageLoadUtil;
import com.pirestupppai.widgets.view.AvatarView;

import butterknife.BindView;
import rx.Subscription;
import rx.functions.Action1;

import static com.pirestupppai.rxbus.RxBus.RXBUS_REFRESH_PROFILE;
import static com.pirestupppai.ui.activity.FollowListActivity.FOLLOW_TYPE_FANS;
import static com.pirestupppai.ui.activity.FollowListActivity.FOLLOW_TYPE_FOLLOW;

/**
 * 个人中心
 *
 * @author: xiaoluo
 * @date: 2017-04-11 10:52
 */
public class ProfileFragment extends BaseFragment {

    @BindView(R.id.profile_avatar)
    AvatarView profileAvatar;
    @BindView(R.id.profile_nickname)
    TextView nicknameTv;
    @BindView(R.id.profile_desc)
    TextView descTv;
    @BindView(R.id.profile_follow_count_tv)
    TextView followCountTv;
    @BindView(R.id.profile_fans_count_tv)
    TextView fansCountTv;
    @BindView(R.id.profile_like_count_tv)
    TextView likeCountTv;
    @BindView(R.id.profile_collect_count_tv)
    TextView collectCountTv;
    @BindView(R.id.profile_ps_count_tv)
    TextView psCountTv;
    @BindView(R.id.profile_message_count_tv)
    TextView messageCountTv;
    @BindView(R.id.profile_tutti_count_tv)
    TextView tuttiCountTv;

    @BindView(R.id.profile_info_layout)
    RelativeLayout infoLayout;
    @BindView(R.id.profile_follow_layout)
    LinearLayout followLayout;
    @BindView(R.id.profile_fans_layout)
    LinearLayout fansLayout;
    @BindView(R.id.profile_like_layout)
    LinearLayout likeLayout;
    @BindView(R.id.profile_level_layout)
    RelativeLayout levelLayout;
    @BindView(R.id.profile_msg_layout)
    RelativeLayout msgLayout;
    @BindView(R.id.profile_ps_layout)
    RelativeLayout psLayout;
    @BindView(R.id.profile_tutti_layout)
    RelativeLayout tuttiLayout;
    @BindView(R.id.profile_collect_layout)
    RelativeLayout collectLayout;
    @BindView(R.id.profile_setting_layout)
    RelativeLayout settingLayout;
    @BindView(R.id.status_bar)
    View statusBar;

    private Subscription mSubscription;

    @Override
    protected Context getFragmentContext() {
        return getActivity();
    }

    @Override
    protected int loadParentLayout() {
        return R.layout.fragment_profile;
    }

    @Override
    protected void initViews() {
        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) statusBar.getLayoutParams();
        params.height = Constant.HEIGHT_OF_STATUSBAR;
        params.width = Constant.WIDTH_OF_SCREEN;
        statusBar.setLayoutParams(params);

        getData();

    }

    private void getData() {
        NetworkRequest.getInstance().getUserData(LoginUser.getInstance().getId(), mContext,
                new TupaiSubscriber<TupaiUser>(mContext) {
                    @Override
                    protected void requestSuccess(TupaiUser loginUser) {
                        LoginUser.getInstance().initFromResponse(loginUser);
                        refreshData();
                    }

                    @Override
                    protected void requestError(String error) {
                        refreshData();
                    }
                });
    }

    private void refreshData() {
        nicknameTv.setText(LoginUser.getInstance().getNickname());
        descTv.setText(LoginUser.getInstance().getSignature());
        followCountTv.setText(LoginUser.getInstance().getFollowed_count() + "");
        fansCountTv.setText(LoginUser.getInstance().getFollower_count() + "");
        likeCountTv.setText(LoginUser.getInstance().getLiked_count() + "");
        messageCountTv.setText(LoginUser.getInstance().getUnread_message_count() + "");

        profileAvatar.setUid(LoginUser.getInstance().getId(), false);
        ImageLoadUtil.getInstance().loadAvatar(LoginUser.getInstance().getAvatar(), profileAvatar);
    }

    @Override
    protected void initListeners() {
        infoLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, UserActivity.class);
                intent.putExtra(Constant.IntentKey.UID, LoginUser.getInstance().getId());
                mContext.startActivity(intent);
            }
        });

        followLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, FollowListActivity.class);
                intent.putExtra(Constant.IntentKey.FOLLOW_LIST_TYPE, FOLLOW_TYPE_FOLLOW);
                intent.putExtra(Constant.IntentKey.UID, LoginUser.getInstance().getId());
                startActivity(intent);
            }
        });

        fansLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, FollowListActivity.class);
                intent.putExtra(Constant.IntentKey.FOLLOW_LIST_TYPE, FOLLOW_TYPE_FANS);
                intent.putExtra(Constant.IntentKey.UID, LoginUser.getInstance().getId());
                startActivity(intent);
            }
        });

        likeLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        levelLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        msgLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                messageCountTv.setText("0");
                startActivity(new Intent(mContext, MessageListActivity.class));
            }
        });

        collectLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, CollectionActivity.class));
            }
        });

        settingLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(mContext, SettingActivity.class));
            }
        });

        psLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(mContext, PsListActivity.class);
                intent.putExtra(Constant.IntentKey.PS_TYPE, PsListAdapter.PS_TYPE_NORMAL);
                startActivity(intent);
            }
        });

        tuttiLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void initLogic() {
        //注册观察者
        mSubscription = RxBus.getInstance()
                .toObservable(RXBUS_REFRESH_PROFILE, String.class)
                .subscribe(new Action1<String>() {
                    @Override
                    public void call(String s) {
                        if (TextUtils.isEmpty(s)) {
                            refreshData();
                        } else {
                            getData();
                        }
                    }
                });
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        if (!mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
        }
    }
}
